#include "GameScene.h"
namespace fishingjoy
{
	namespace scene
	{
		IGameScene* createScene()
		{
			return CGameScene::create();
		}

		void CGameScene::attachTo(cocos2d::CCNode* node)
		{
			node->addChild(this);
		}

		void CGameScene::setBackground( const char* pFile )
		{
			if(m_pBg){
				removeChild(m_pBg,true);
				m_pBg = NULL;
			}
			cocos2d::CCSprite* pS = cocos2d::CCSprite::create(pFile);
			pS->setAnchorPoint(cocos2d::CCPointZero);

			m_pBgLayer->addChild(pS);
			m_pBg = pS;
		}

		ISceneObject* CGameScene::createObject(int tid)
		{
			CSceneObject* pObj = new CSceneObject(tid);
			m_vecObjectsList.push_back(pObj);
			pObj->getRenderObject()->setPosition(cocos2d::CCPointMake(150,150));
			m_pObjectLayer->addChild(pObj->getRenderObject());
			return pObj;
		}


		void CGameScene::destroyObject( ISceneObject* pObj )
		{
			if(pObj){
				std::vector<CSceneObject*>::iterator it = m_vecObjectsList.begin();
				for(;it!=m_vecObjectsList.end();++it){
					if(*it == pObj){
						m_vecObjectsList.erase(it);
						m_pObjectLayer->removeChild(((CSceneObject*)pObj)->getRenderObject(),true);
						delete ((CSceneObject*)pObj);
						return;
					}
				}
			}
		}


		void CGameScene::update( float dt )
		{
			std::vector<CSceneObject*>::iterator it = m_vecObjectsList.begin();
			std::vector<CSceneObject*>::iterator end = m_vecObjectsList.end();
			for(;it!=end;++it){
				(*it)->update(dt);
			}
		}

		void CGameScene::setSceneInfo( int w,int h,int extend)
		{
			m_pObjectLayer->setPosition(cocos2d::CCPointMake(-extend,-extend));
		}

	}
}